package com.tastyshop.controller;

import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.tastyshop.domain.RecommendBoard;
import com.tastyshop.domain.Recommend_Reply;
import com.tastyshop.service.RecommendService;

@Controller
public class RecommendController {
	
	@Autowired
	private RecommendService service;
	
	@RequestMapping(value="recommend.do", method = RequestMethod.GET)
	public String recommend(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
			Model model){
		model.addAttribute("page", page);
		model.addAttribute("lastPage", service.getLastPage());
		model.addAttribute("boards", service.getAllBoardByPage(page));
		return "recommend";
	}
	
	@RequestMapping(value="recom_detail.do", method = RequestMethod.GET)
	public String recom_detail(@RequestParam(value = "idx") Integer idx, HttpSession session,
			 Model model){
		HashMap<String, RecommendBoard> map = service.getInfo(idx);
		String sessionUser = (String) session.getAttribute("nick_name");
		service.raiseLookupCount(idx);
		RecommendBoard recommendBoard = map.get("recommendBoard");
		recommendBoard.setContent(recommendBoard.getContent().replaceAll("\n", "<br>"));
		model.addAttribute("sessionUser", sessionUser);
		model.addAttribute("prev", map.get("prev"));
		model.addAttribute("next", map.get("next"));
		model.addAttribute("board", recommendBoard);
		model.addAttribute("replies", service.getAllRepliesByIdx(idx));
		model.addAttribute("reply", new Recommend_Reply());
		return "recom_detail";
	}
	
	@RequestMapping(value = "recom_write.do", method = RequestMethod.GET)
	public String recom_insert(@RequestParam(value = "idx", required = false) Integer idx,
			@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
			@RequestParam(value = "action") String action, Model model, HttpSession session){
		if(action.equals("new")){
			if(session.getAttribute("email") == null){
				return "login";
			}
			model.addAttribute("title", "새 글 작성");
			model.addAttribute("board", new RecommendBoard());
		}else if(action.equals("update")){
			model.addAttribute("title", "편집");
			model.addAttribute("board", service.getBoardByIdx(idx));
		}else if(action.equals("delete")){
			service.removeBoard(idx);
			return "redirect:recommend.do?page=" + page;
		}else{
			throw new RuntimeException();
		}
		model.addAttribute("page", page);
		model.addAttribute("action", action);
		return "recom_write";
	}
	
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public String saveBoard(@RequestParam(value = "idx") Integer idx, 
			@RequestParam(value = "page", required = false,	defaultValue = "1") Integer page,
			@RequestParam(value = "action") String action, 
			@RequestParam(value = "writer") String writer, RecommendBoard recommendBoard, Recommend_Reply recommend_Reply){
		System.out.println(action);
		System.out.println(writer);
		if(action.equals("new")){
			service.addBoard(recommendBoard);
			return "redirect:recommend.do?page=" + page;
		}else if(action.equals("update")){
			recommendBoard.setIdx(idx);
			service.modifyBoard(recommendBoard);
			return "redirect:recom_detail.do?idx=" + idx + "&page=" + page;
		}else if(action.equals("reply")){
			recommend_Reply.setRef_no(idx);
			service.addReply(recommend_Reply);
			return "redirect:recom_detail.do?idx=" + idx + "&page=" + page;
		}else{
			throw new RuntimeException();
		}
	}
	
	@RequestMapping(value = "reply.do", method = RequestMethod.GET)
	public String replyModifyDel(@RequestParam(value = "idx") Integer idx, 
			@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
			@RequestParam(value = "rno") Integer rno, @RequestParam(value = "action") String action,
			Recommend_Reply recommend_Reply, Model model){
		System.out.println(idx);
		System.out.println(page);
		System.out.println(rno);
		System.out.println(action);
		if(action.equals("moify_repl")){
			recommend_Reply.setIdx(rno);
			service.modifyReply(recommend_Reply);
			return "redirect:recom_detail.do?idx=" + idx + "&page=" + page;
		}else if(action.equals("del_repl")){
			recommend_Reply.setIdx(rno);
			recommend_Reply.setRef_no(idx);
			service.removeReply(recommend_Reply);
			return "redirect:recom_detail.do?idx=" + idx + "&page=" + page;
		}else{
			throw new RuntimeException();
		}
		
	}
}
